package com.luck.pf.security.dept.web;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONObject;

import org.apache.commons.lang.StringUtils;

import com.luck.org.entity.SysDepartment;
import com.luck.pf.common.Constants;
import com.luck.pf.common.entity.UserBean;
import com.luck.pf.framework.action.BaseActionSupport;
import com.luck.pf.security.dept.logic.IDeptLogic;
import com.luck.pf.security.dept.qry.DeptQry;
import com.luck.pf.util.DateUtil;
import com.luck.pf.util.ObjectUtil;
import com.luck.pf.util.Page;

/**
 * 说明:
 * Copyright: Copyright (c) 2010
 * @author zhangwr 2011-8-11 上午09:40:33
 * @version 1.0
 */
public class DeptAction extends BaseActionSupport {
	private static final long serialVersionUID = 716080692269504982L;
	IDeptLogic deptLogic;
	DeptQry qry;
	SysDepartment entity;
	List<SysDepartment> depts;
	Page page;
	public String index() throws Exception{
		return "index";
	}
	public String tree() throws Exception{
		UserBean userBean = super.getUserInfo();
		if (StringUtils.isNotBlank(super.qryHex)) {
			qry = (DeptQry)ObjectUtil.getObjectFromHex(qryHex);
		}
		if (qry == null) {
			qry = new DeptQry();
		}
		qry.setUserInfo(userBean);
		depts = deptLogic.findDepts(qry);
		super.title="机构树";
		return "tree";
	}
	public String list() throws Exception{
		UserBean userBean = super.getUserInfo();
		if (StringUtils.isNotBlank(super.qryHex)) {
			qry = (DeptQry)ObjectUtil.getObjectFromHex(qryHex);
		}
		if (qry == null) {
			qry = new DeptQry();
		}
		qry.setUserInfo(userBean);
		page = deptLogic.findDeptPage(qry);
		super.title="机构列表";
		return "list";
	}
	public String select() throws Exception{
		UserBean userBean = super.getUserInfo();
		if (StringUtils.isNotBlank(super.qryHex)) {
			qry = (DeptQry)ObjectUtil.getObjectFromHex(qryHex);
		}
		if (qry == null) {
			qry = new DeptQry();
		}
		qry.setUserInfo(userBean);
		depts = deptLogic.findDepts(qry);
		super.title="机构树";
		return "select";
	}
	public String checkDept() throws Exception {	
		boolean isCorrect = deptLogic.checkDept(entity);
		super.rendText(String.valueOf(isCorrect));
		return null;
	}
	public String toAdd() throws Exception{
		//entity = new SysDepartment();
		entity.setId(ADD_PK);
		super.title="增加机构";
		return "edit";
	}
	public String toEdit() throws Exception{
		UserBean userBean = super.getUserInfo();
		entity = deptLogic.get(SysDepartment.class, entity.getId());
		super.title="修改机构";
		return "edit";
	}
	public String edit() throws Exception{
		SysDepartment parent = new SysDepartment();
		UserBean userBean = super.getUserInfo();
		if(entity.getParentId()!=null)
			parent = deptLogic.get(SysDepartment.class, entity.getParentId());
		else{
			entity.setDeptKind("1");
			entity.setDeptLevel("0");
			entity.setTreeNo("101");
		}
		entity.setOperTime(DateUtil.getDate());
		entity.setOperUser(userBean.getUserId());
		String str = "";
		//如果不支持多机构管理
		if(Constants.DEPT_MANY.equals("0")){
			if(entity.getParentId()!=null){//如果父机构不为空，则为子
				entity.setDeptKind("0");
				entity.setDeptLevel("1");
			}
		}
		try {
			if(entity.getId() == ADD_PK){//新增
				entity.setId(null);
				entity.setParentId(entity.getParentId());
				entity = deptLogic.saveDept(entity,userBean);
			}else{//修改
				SysDepartment db = deptLogic.get(SysDepartment.class, entity.getId());
				super.copyProperties(db, entity);
				entity = deptLogic.saveDept(db,userBean);
			}	
		} catch (Exception e) {
			str = e.getMessage();
		}
		Map<String, String> jsonMap = new HashMap<String, String>();
		if(StringUtils.isNotBlank(str)){
			jsonMap.put("status", ERROR);
			jsonMap.put("message", str);
		}else{
			jsonMap.put("deptid", entity.getId().toString());
			jsonMap.put("status", SUCCESS);
			jsonMap.put("message", "操作成功");
		}
		JSONObject jsonObject = JSONObject.fromObject(jsonMap);
		this.rendText(jsonObject.toString());
		return null;
	}
	public String delete() throws Exception{
		UserBean userBean = super.getUserInfo();
		entity = deptLogic.get(SysDepartment.class, entity.getId());
		Long pId = entity.getParentId();
		String str = "";
		try {
			str = deptLogic.deleteDept(entity,userBean, createLog(userBean, "删除机构["+entity.getDeptName()+"]", "2"));
		} catch (Exception e) {
			str = e.getMessage();
		}
		if(StringUtils.isBlank(str)){
			Map<String, String> jsonMap = new HashMap<String, String>();
			jsonMap.put("status", SUCCESS);
			jsonMap.put("deptid", pId.toString());
			jsonMap.put("message", "操作成功");
			JSONObject jsonObject = JSONObject.fromObject(jsonMap);
			this.rendText(jsonObject.toString());
		}else
			super.renderJsonError(str+"不能删除！");
		return null;
	}
	public String view() throws Exception{
		entity = deptLogic.get(SysDepartment.class, entity.getId());
		super.title="查看机构";
		return "view";
	}
	public IDeptLogic getDeptLogic() {
		return deptLogic;
	}
	public void setDeptLogic(IDeptLogic deptLogic) {
		this.deptLogic = deptLogic;
	}
	public DeptQry getQry() {
		return qry;
	}
	public void setQry(DeptQry qry) {
		this.qry = qry;
	}
	public SysDepartment getEntity() {
		return entity;
	}
	public void setEntity(SysDepartment entity) {
		this.entity = entity;
	}
	public List<SysDepartment> getDepts() {
		return depts;
	}
	public void setDepts(List<SysDepartment> depts) {
		this.depts = depts;
	}
	public Page getPage() {
		return page;
	}
	public void setPage(Page page) {
		this.page = page;
	}
	
}
